package com.tanovo.api;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tanovo.bo.ResultObject;
import com.tanovo.bo.RoleFindBo;
import com.tanovo.bo.RoleResourceBindBo;
import com.tanovo.bo.UserCore;
import com.tanovo.config.UserInfoContext;
import com.tanovo.domain.Role;
import com.tanovo.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "角色")
public class RoleApi {

    @Autowired
    private IRoleService iRoleService;

    @PostMapping("/role/add")
    @ApiOperation(value = "添加角色", notes = "添加角色")
    public ResultObject addNewUser(@RequestBody Role role) {
        UserCore user = UserInfoContext.getUser();
        role.setCompanyId(user.getCompanyId());
        role.setClientId(user.getClientId());
        role.setUpdateBy(user.getId().toString());
        role.setCreateBy(user.getId().toString());
        iRoleService.addRole(role);
        return new ResultObject();
    }

    @GetMapping("/role/delete")
    @ApiOperation(value = "删除角色", notes = "删除角色")
    public ResultObject delete(@RequestParam Integer roleId) {
        iRoleService.delete(roleId);
        return new ResultObject();
    }
    @PostMapping("/role/delete/list")
    @ApiOperation(value = "批量删除角色", notes = "批量删除角色")
    public ResultObject deleteRoles(@RequestBody List<Integer> roleIdList) {
        iRoleService.deleteRoleList(roleIdList);
        return new ResultObject();
    }

    @PostMapping("/role/update")
    @ApiOperation(value = "修改编辑角色", notes = "修改编辑角色")
    public ResultObject editRole(@RequestBody Role role) {
        iRoleService.editRole(role);
        return new ResultObject();
    }

    @PostMapping("/role/list")
    @ApiOperation(value = "角色列表+分页", notes = "角色列表+分页")
    public ResultObject<PageInfo<Role>> list(@RequestBody RoleFindBo roleFindBo) {
        PageHelper.startPage(roleFindBo.getPageNum(), roleFindBo.getPageSize());
        UserCore user = UserInfoContext.getUser();
        roleFindBo.setCompanyId(user.getCompanyId());
        roleFindBo.setClientId(user.getClientId());
        roleFindBo.setUserId(user.getId());
        List<Role> list = iRoleService.getRolelist(roleFindBo);
        PageInfo<Role> pageInfo = new PageInfo<>(list);
        return new ResultObject(pageInfo);
    }

    @PostMapping("/role/checked/list")
    @ApiOperation(value = "选中的角色列表", notes = "选中的角色列表")
    public ResultObject<Role> checkedList(@RequestBody RoleFindBo roleFindBo) {
        UserCore user = UserInfoContext.getUser();
        roleFindBo.setCompanyId(user.getCompanyId());
        roleFindBo.setClientId(user.getClientId());
        List<Role> list = iRoleService.getCheckedRolelist(roleFindBo);
        return new ResultObject(list);
    }

    @PostMapping("/role/bindResources")
    @ApiOperation(value = "角色绑定资源", notes = "角色绑定资源")
    public ResultObject bindResources(@RequestBody RoleResourceBindBo roleResourceBindBo) {
        iRoleService.bindResources(roleResourceBindBo);
        return new ResultObject();
    }

    @PostMapping("/role/initAdmin")
    @ApiOperation(value = "初始化添加应用管理员", notes = "初始化添加应用管理员")
    public ResultObject initAdmin() {
        UserCore user = UserInfoContext.getUser();
        iRoleService.initAdmin(user);
        return new ResultObject();
    }
}
